﻿// Global variables
var SelectedListObject;
// End of global variables

// Call init function
init();

// Init functions
function init() {
    // attach the ribbon css file using js
    var fileref = document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", "/_layouts/SharePointSocialNetworking/SharePointSocialNetworking.css");
    document.getElementsByTagName("head")[0].appendChild(fileref)
}
// End init functions


// All the social networking commands starts here
function SendToFacebook() {
    SP.UI.Notify.addNotification('Sending to Facebook...');
    window.open('http://www.facebook.com/share.php?u=' + encodeURI(window.location));
}

function SendToTwitter() {
    SP.UI.Notify.addNotification('Sending to Twitter...');
    window.open('http://twitter.com/home?status=' + encodeURI(window.location));
}

function SendTag() {
    SafeRunFunction(function () { TagDialogOpener.Open(encodeURI(window.location), document.title, '0'); }, 'SocialData.js', 'TagDialogOpener');
}

function SendToLive() {
    SP.UI.Notify.addNotification('Sending to Live...');
    window.open('https://favorites.live.com/quickadd.aspx?marklet=1&url=' + encodeURIComponent(window.location));
}

function SendToGoogle() {
    SP.UI.Notify.addNotification('Sending to Google...');
    window.open('http://www.google.com/bookmarks/mark?op=edit&bkmk=' + encodeURIComponent(window.location) + '&title=' + document.title);
}

function SendToReddit() {
    SP.UI.Notify.addNotification('Sending to Reddit...');
    window.open('http://reddit.com/submit?url=' + encodeURIComponent(window.location) + '&title=' + doc.title);
}

function SendToTechnorati() {
    SP.UI.Notify.addNotification('Sending to Technorati...');
    window.open('http://technorati.com/faves?add=' + encodeURIComponent(window.location));
}

function SendToDelicious() {
    SP.UI.Notify.addNotification('Sending to Del.icio.us...');
    window.open('http://delicious.com/post?url=' + encodeURIComponent(window.location) + '&title=' + document.title + '&notes=');
}

function SendToLinkedIn() {
    SP.UI.Notify.addNotification('Sending to LinkedIn...');
    window.open('http://www.linkedin.com/shareArticle?mini=true&url=' + encodeURIComponent(window.location) + '&title=' + document.title + '&ro=false&summary=&source=');
}

function SendToDiggIt() {
    SP.UI.Notify.addNotification('Sending to Digg...');
    window.open('http://digg.com/submit?phase=2&url=' + encodeURIComponent(window.location) + '&title=' + document.title + '&bodytext=');
}

function SendToStumbleUpon() {
    window.open('http://www.stumbleupon.com/submit?url=' + encodeURIComponent(window.location) + '&title=' + document.title);
}

function SendToFF() {
    window.open('http://www.friendfeed.com/share?title=' + document.title + '&link=' + encodeURIComponent(window.location));
}
// end of social networking commands

// start of EnabledScripts

function EnableEmailButton() {
    // request number of selected items. 
    var items = SP.ListOperation.Selection.getSelectedItems();
    var count = CountDictionary(items);
    // only return true if a single item is selected. 
    return (count == 1);
}

function EnableRSSButton() {
    // request the guid of selected list. if the page is not a list, then the RSS button wont be activated.
    var list = SP.ListOperation.Selection.getSelectedList();
    if (list != null)
        return true;
    return false; 
}

// End of EnabledScripts

// start of email and rss commands

function EmailLink() {
    // get the context and web objects
    var ctx = SP.ClientContext.get_current();
    this.web = ctx.get_web();
    this.site = ctx.get_site();
    // Get the current selected list, then load the list using the getById method of the web object.
    var listId = SP.ListOperation.Selection.getSelectedList();
    SelectedListObject = this.web.get_lists().getById(listId);
    // Get all the selected items of our list. This will return an ienumerable with ONLY an id field
    var items = SP.ListOperation.Selection.getSelectedItems(ctx);
    var firstItem = items[0];
    // Request the list item from the server using the getItemById method. This will load all the properties of the item unless we pre-requested   
    // fields on the list load method to save bandwidth.
    this.listItem = SelectedListObject.getItemById(firstItem.id);
    // load the item in the context
    ctx.load(SelectedListObject);
    ctx.load(this.listItem);
    ctx.load(this.site);
    //Execute all the commands and acctually start things rolling on the server side. Specify delegates to handle the response from the server. 
    ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function GetRSSFeed() {
    var ctx = SP.ClientContext.get_current();
    this.web = ctx.get_web();
    ctx.load(this.web);
    //Execute all the commands and acctually start things rolling on the server side. Specify delegates to handle the response from the server. 
    ctx.executeQueryAsync(Function.createDelegate(this, this.onRSSQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

// end of email and rss commands

// start of delegates

function onQuerySucceeded(sender, args) {
    var url = encodeURIComponent(this.site.get_url() + SelectedListObject.get_defaultDisplayFormUrl() + "?id=" + this.listItem.get_item('ID'));
    // Request the name of the document. 
    var title = this.listItem.get_item('Title');
    // Open a new e-mail in the default mail program.
    window.location = 'mailto:?subject=Emailing Link For%3A%20' + title + '&body='+ url;
}

function onRSSQuerySucceeded(sender, args) {
    var link = this.web.get_serverRelativeUrl() + "/_layouts/listfeed.aspx?List=" + encodeURIComponent(SP.ListOperation.Selection.getSelectedList());
    window.open(link);
}

function onQueryFailed(sender, args) {
    alert('Operation failed for the following reason(s): ' + args.toString());
}

// end of delegates
